package com.jeasonzhao.report.vml.contrib;

import com.jeasonzhao.report.vml.base.LineText;
import com.jeasonzhao.report.vml.base.PolyLine;
import com.jeasonzhao.report.vml.base.Vector2D;
import com.jeasonzhao.report.vml.chart.enums.TextAligns;
import com.jeasonzhao.report.vml.exception.VMLException;


public class TitleBox extends PolyLine
{
    private boolean m_isShownLeftTopCorner = false;
    private boolean m_isShownRightTopCorner = false;
    private boolean m_isShownLeftBottomCorner = false;
    private boolean m_isShownRightBottomCorner = false;
    private boolean m_isShownBox = true;
    private TextAligns m_Align = TextAligns.Center;
    private int m_nCornerSize = 0;

    public TitleBox()
    {
        super();
    }

    public void setShowBox(boolean b)
    {
        m_isShownBox = b;
    }

    public void setAlign(TextAligns a)
    {
        m_Align = a;
    }

    public void setCornerSize(int n)
    {
        this.m_nCornerSize = n;
    }

    public void setByCenter(Vector2D pt)
    {
        setByCenter(pt.getX(),pt.getY());
    }

    public void setByCenter(int x,int y)
    {
        m_nCornerSize = Math.max(m_nCornerSize,0);
        int nw = 0;
        if(m_isShownLeftTopCorner || m_isShownLeftBottomCorner)
        {
            nw += m_nCornerSize;
        }
        if(m_isShownRightTopCorner || m_isShownRightBottomCorner)
        {
            nw += m_nCornerSize;
        }
        int nh = 0;
        if(m_isShownLeftTopCorner || m_isShownRightTopCorner)
        {
            nh += m_nCornerSize;
        }
        if(m_isShownLeftBottomCorner || m_isShownRightBottomCorner)
        {
            nh += m_nCornerSize;
        }
        nh += Math.max(2,m_nCornerSize) * 3;
        nh += m_nFontHeightPixel + 2;

        nw += Math.max(2,m_nCornerSize) * 3;
        nw += TextHelper.getPixelLength(m_strText,m_nFontHeightPixel / 2);

        super.setByCenter(x,y,nw,nh);
    }

    public String getScript()
        throws VMLException
    {
        if(m_nFontHeightPixel <= 0)
        {
            m_nFontHeightPixel = 20;
        }
        if(this.m_nCornerSize <= 0)
        {
            setCorner(false);
        }
        m_nCornerSize = Math.max(m_nCornerSize,0);
        PolyLine path = new PolyLine();
        Vector2D pt = new Vector2D(getTopLeft());
        Vector2D startPos = new Vector2D(getTopLeft());
        int nW = getSize().getWidth();
        int nH = getSize().getHeight();
        //��ʼλ��
        if(m_isShownLeftTopCorner)
        {
            startPos.offset(0,m_nCornerSize);
            pt.offset(0,m_nCornerSize);
            path.add(pt);
            pt.offset(m_nCornerSize,0 - m_nCornerSize);
            path.add(pt);
            nW -= m_nCornerSize;
        }
        else
        {
            path.add(pt);
        }
        //����
        if(m_isShownRightTopCorner)
        {
            nW -= m_nCornerSize;
            path.add(pt.offset(nW,0));
            path.add(pt.offset(m_nCornerSize,m_nCornerSize));
            nH -= m_nCornerSize;
        }
        else
        {
            path.add(pt.offset(nW,0));
        }

        if(m_isShownRightBottomCorner)
        {
            nH -= m_nCornerSize;
            path.add(pt.offset(0,nH));
            path.add(pt.offset(0 - m_nCornerSize,m_nCornerSize));
            nW = getSize().getWidth() - m_nCornerSize;
        }
        else
        {
            pt.offset(0,nH);
            path.add(pt);
            nW = getSize().getWidth();
        }
        if(m_isShownLeftBottomCorner)
        {
            nW -= m_nCornerSize;
            path.add(pt.offset(0 - nW,0));
            path.add(pt.offset(0 - m_nCornerSize,0 - m_nCornerSize));
        }
        else
        {
            pt.offset(0 - nW,0);
            path.add(pt);
        }
        path.add(startPos);
        path.setFilled(this.isFilled());
        path.setFillColor(this.getFillColor());
        path.setStrokeColor(this.getStrokeColor());
        path.setZIndex(this.getZIndex());
        path.addSubVmlElement(getSubElements());
        int nTextLength = TextHelper.getPixelLength(m_strText,m_nFontHeightPixel / 2);
        if(nTextLength <= 0)
        {
            return m_isShownBox ? path.getScript() : "";
        }
        pt = this.getLeftTop();
        int nxo = (this.getWidth() - nTextLength) / 2;
        if(null == m_Align || m_Align.equals(TextAligns.Left))
        {
            nxo = m_nCornerSize;
        }
        else if(m_Align.equals(TextAligns.Right))
        {
            nxo = this.getWidth() - nTextLength - m_nCornerSize;
        }
        pt.offset(nxo,this.getHeight() / 2);

        LineText lineText = TextHelper.getHorizonTextLine(m_strText,pt,nTextLength);
        //lineText.setStrokeColor(null == m_strTextColor || m_strTextColor.trim().length() < 1 ? getStrokeColor() : m_strTextColor);
        lineText.setZIndex(this.getZIndex());
        return m_isShownBox == false ? lineText.getScript() :
            path.getScript() + lineText.getScript();
    }

    private String m_strText = null;
    private int m_nFontHeightPixel = 20;
    public void setText(String strText)
    {
        m_strText = strText;
    }

    public void setFontHeightPixel(int n)
    {
        m_nFontHeightPixel = n;
    }

    public void setLeftBottomCorner(boolean leftBottomCorner)
    {
        this.m_isShownLeftBottomCorner = leftBottomCorner;
    }

    public void setLeftTopCorner(boolean leftTopCorner)
    {
        this.m_isShownLeftTopCorner = leftTopCorner;
    }

    public void setRightBottomCorner(boolean rightBottomCorner)
    {
        this.m_isShownRightBottomCorner = rightBottomCorner;
    }

    public void setRightTopCorner(boolean rightTopCorner)
    {
        this.m_isShownRightTopCorner = rightTopCorner;
    }

    public int getCornerSize()
    {
        return this.m_nCornerSize;
    }

    public void setCorner(boolean b)
    {
        setCorner(this.m_nCornerSize,b);
    }

    public void setCorner(int n,boolean b)
    {
        m_nCornerSize = n;
        this.m_isShownLeftTopCorner = b;
        this.m_isShownRightTopCorner = b;
        this.m_isShownLeftBottomCorner = b;
        this.m_isShownRightBottomCorner = b;
    }

    public void setWidthByText(int nFontSizeHeight)
    {
        this.setWidth(TextHelper.getPixelLength(this.m_strText,nFontSizeHeight / 2));
    }
}
